const BASE_URL = "https://master.gddzxcx.com/api/";
import {toast} from '../utils/Tools'
const app = getApp()

export default params => {
  // wx.showLoading({
  //   title: "正在加载中...",
  // })
  let contentType;
  return new Promise((resolve, reject) => {
    if (params.method === 'POST') {
      contentType = 'application/x-www-form-urlencoded';
    } else {
      contentType = 'application/json'
    }
    const data = Object.assign({},params.data,{'token':wx.getStorageSync("token")|| ''})
    const login_status =  wx.getStorageSync('login_status') || 'business';
    if(login_status === 'business'){
      wx.request({
        url: BASE_URL + params.url,
        data,
        timeout:30000,
        method: params.method || "GET",
        header:{
          'content-type': contentType,
          'Authorization':"Bearer " + wx.getStorageSync("token") || ''
        },
        success:res => {
          console.log(res)
          if (res.data) {
            /** start 根据需求 接口的返回状态码进行处理 */
            if (res.statusCode == 200) {
              if(res.data.code === 200){
                resolve(res.data)
              }else if(res.data.code === 404){
                toast('检测到未登录, 请先登录')
                setTimeout(()=>{
                  wx.reLaunch({
                    url: '/pages/other/register/register'
                  });
                },1500)
                return;
              }else if(res.data.code===500){
               
                if(res.data.message==='你没有登陆！拒绝登录'||res.data.message==='你没有通过token！拒绝登录'){
                  toast('检测到未登录, 请先登录')
                setTimeout(()=>{
                  wx.reLaunch({
                    url: '/pages/other/register/register'
                  });
                },1500)
                }else{
                  resolve(res.data)
                }
              }else{
                resolve(res.data)
              }
            } else {
              reject(res.data.message); //request failed
            }
            /** end 处理结束*/
          }
        },
        fail: error => {
          reject(error)
          toast('服务器错误,请稍后重试','none',2000)
        }
      })
      // wx.hideLoading();
    }
  })
}
